﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UINumberImg : MonoBehaviour
{
    public int mNumber;
    public Sprite[] mNumberSpAry;
    public SpriteRenderer[] mSpAry;
    public Image[] mNumberAry;          //
    public bool mIsReversal;
    public bool mIsNumberFull;
    public const int ascillOffect = -48;

    public GameObject Target;

    /// <summary>
    /// 数值发生改变事回调
    /// </summary>
    public Action<GameObject> mChangeValue;


#if UNITY_EDITOR
    public bool mTestIsNumber;
#endif
    // Use this for initialization

    private void Awake()
    {
       
        
        //if (mNumberAry == null)
        //{
        //    mNumberAry = transform.GetComponentsInChildren<Image>();
        //}
        //else if (mSpAry == null)
        //{
        //    mSpAry = transform.GetComponentsInChildren<SpriteRenderer>();
        //}

    }

    public void OnEnable()
    {
        SetNumber(mNumber);
    }

    private void Start()
    {

    }

#if UNITY_EDITOR
    private void Update()
    {
        if (mTestIsNumber)
        {
            SetNumber(mNumber);
            mTestIsNumber = false;
        }
    }

#endif

    public void SetNumber(int number)
    {
        if (number <= 0)
            number = 0;

        mNumber = number;

        if (mNumberAry != null && mNumberAry.Length > 0)
            SetNumberImage(number.ToString());
        else if (mSpAry != null && mSpAry.Length > 0)
            SetNumberSprite(number.ToString());
    }

    private void SetNumberImage(string number)
    {
        if (mChangeValue != null)
            mChangeValue(Target);

        if (mNumberSpAry == null || mNumberSpAry.Length == 0)
            return;


        foreach (var item in mNumberAry)
        {
            item.sprite = null;
            item.color = Color.clear;
        }

        Sprite s = null;
        int ascll = 0;
        Image numIm = null;
        for (int i = 0; i < number.Length; i++)
        {
            if (i >= mNumberAry.Length && mIsNumberFull == false)
                break;

            if (mIsReversal)
            {
                ascll = System.Convert.ToInt32(number[number.Length - 1 - i]) + ascillOffect;
                numIm = mNumberAry[mNumberAry.Length - 1 - i];
            }
            else
            {
                ascll = System.Convert.ToInt32(number[i]) + ascillOffect;
                numIm = mNumberAry[i];
            }

            s = mNumberSpAry[ascll];

            if (i >= mNumberAry.Length && mIsNumberFull == true)
            {
                numIm.sprite = mNumberSpAry[0];
                numIm.color = Color.white;
            }
            else
            {
                numIm.sprite = s;
                numIm.color = Color.white;

            }
        }

        //for (int i = 0; i < mNumberAry.Length; i++)
        //{
        //    if (i >= number.Length)
        //    {
        //        mNumberAry[i].sprite = mNumberSpAry[0];
        //        mNumberAry[i].color = Color.white;
        //    }
        //}

    }

    private void SetNumberSprite(string number)
    {
        if (mChangeValue != null)
            mChangeValue(Target);

        foreach (var item in mSpAry)
        {
            item.sprite = null;
            item.color = Color.clear;
        }

        Sprite s = null;
        int ascll = 0;
        SpriteRenderer numIm = null;
        for (int i = 0; i < number.Length; i++)
        {
            if (i >= mNumberSpAry.Length && mIsNumberFull == false)
                break;

            if (mIsReversal)
            {
                ascll = System.Convert.ToInt32(number[number.Length - 1 - i]) + ascillOffect;
                numIm = mSpAry[mSpAry.Length - 1 - i];
            }
            else
            {
                ascll = System.Convert.ToInt32(number[i]) + ascillOffect;
                numIm = mSpAry[i];
            }

            s = mNumberSpAry[ascll];

            if (i >= mNumberSpAry.Length && mIsNumberFull == true)
            {
                numIm.sprite = mNumberSpAry[0];
                numIm.color = Color.white;
            }
            else
            {
                numIm.sprite = s;
                numIm.color = Color.white;

            }
        }
    }

}
